#!/bin/bash

# 设置Java运行环境
if [ -z "$JAVA_HOME" ]; then
    echo "Error: JAVA_HOME not set!"
    echo "Please set JAVA_HOME environment variable."
    exit 1
fi

# 获取端口参数
PORT=8080
if [ ! -z "$1" ]; then
    PORT=$1
fi

# 添加执行权限
chmod +x ./gradlew

echo "Building server jar..."
./gradlew server-web:clean server-web:build

if [ $? -ne 0 ]; then
    echo "Failed to build server jar!"
    exit 1
fi

echo "Starting Privacy API Log Server..."
echo "Port: $PORT"

# 运行服务器
"$JAVA_HOME/bin/java" -jar server-web/build/libs/server-web.jar $PORT

if [ $? -ne 0 ]; then
    echo "Failed to start server!"
    exit 1
fi
